package com.sander.guardmanagement.ui.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.HomeDeleteRuzhuren;
import com.sander.guardmanagement.http.HomeGetRuzhurenList;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.response.ResponseHomeGetRuzhurenList;
import com.sander.guardmanagement.response.ResponseHomeGetRuzhurenListData;
import com.sander.guardmanagement.response.ResponseHomeGetUserHomeListData;
import com.sander.guardmanagement.response.ResponseModel;
import com.sander.guardmanagement.until.UserInfoManager;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.ArrayList;
import java.util.List;

public class OccupantManagementActivity extends AppCompatActivity {
    ResponseHomeGetUserHomeListData homedata;
    List<ResponseHomeGetRuzhurenListData> ruzhurenListDatas = new ArrayList<>();
    OccupantListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_occupant_management);
        Intent intent = getIntent();
        homedata = (ResponseHomeGetUserHomeListData) intent.getSerializableExtra("ruzhuren");
        TextView address = (TextView) findViewById(R.id.addresscontent);
        address.setText(homedata.getAdress());
        initCallBack();
        initData();
        initView();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initData(){
        HomeGetRuzhurenList homeGetRuzhurenList = new HomeGetRuzhurenList();
        homeGetRuzhurenList.uid = UserInfoManager.sharedManager().getUserInfo().getId() + "";
        homeGetRuzhurenList.home_id = homedata.id + "";
        OkHttpHelper.getinstance().post(homeGetRuzhurenList.requestUrl(), homeGetRuzhurenList.getValueOfClas(), new BaseCallback<ResponseHomeGetRuzhurenList>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseHomeGetRuzhurenList o) {
                Log.d("sander","->" + o.getMsg());
                if (o.getCode() == 0){
                    List<ResponseHomeGetRuzhurenListData> list = o.getData();
                    ruzhurenListDatas = list;
                    updatelistData();
                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });



    }

    public void updatelistData(){
        adapter.notifyDataSetChanged();;
    }
    public void initView(){
        ListView listView = (ListView) findViewById(R.id.adress_listview);

        adapter = new OccupantListAdapter();
        listView.setAdapter(adapter);


        Button button = (Button ) findViewById(R.id.ruzhurennewbutton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("sander","新建一个入住人");
                Intent intent = new Intent(OccupantManagementActivity.this,AddNewPeopleActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("ruzhuren",homedata);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }
    public void showAlertView(final int postion)
    {
        new MaterialDialog.Builder(this)
                .title("提示")
                .content("确定删除吗？")
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                        deletedAdress(postion);
                    }
                })
                .show();

        Log.d("sander","确定删除了吧");

    }

    private deletedListClick demelteClick = new deletedListClick() {
        @Override
        public void onclickManager(int position, View view) {
            Log.d("sander","删除");
            ResponseHomeGetRuzhurenListData data = ruzhurenListDatas.get(position);

            showAlertView(position);
        }
    };
    public void deletedAdress(final int position){
        ResponseHomeGetRuzhurenListData data = ruzhurenListDatas.get(position);
        HomeDeleteRuzhuren homeDeleteRuzhuren = new HomeDeleteRuzhuren();
        homeDeleteRuzhuren.uid = UserInfoManager.sharedManager().getUserInfo().getId();
        homeDeleteRuzhuren.ruzhuren_id = data.id;
        homeDeleteRuzhuren.home_id = homedata.id;

        OkHttpHelper.getinstance().post(homeDeleteRuzhuren.requestUrl(), homeDeleteRuzhuren.getValueOfClas(), new BaseCallback<ResponseModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseModel o) {
                if (o.getCode() == 0){
                    ruzhurenListDatas.remove(position);
                    updatelistData();
                }else{
                    showToastMessage(o.getMsg());
                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });

    }

    public void showToastMessage(String message){
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

    }
    private class OccupantListAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return ruzhurenListDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            OccupantItem item = null;
            if (convertView == null){
                LayoutInflater infalter = getLayoutInflater();
                convertView =  infalter.inflate(R.layout.occupantitem,parent,false);
                item = new OccupantItem();
                item.textView = (TextView) convertView.findViewById(R.id.occupantTitleLabel);
                item.button = (Button)convertView.findViewById(R.id.deletedation);
                convertView.setTag(item);
            }else{
                item = (OccupantItem)convertView.getTag();
            }
            ResponseHomeGetRuzhurenListData data = ruzhurenListDatas.get(position);
            item.textView.setText(data.getruzhureninfo());
            item.button.setOnClickListener(demelteClick);
            item.button.setTag(position);
            return convertView;
        }
    }
    private class OccupantItem {
        TextView textView;
        Button button;
    }
    public static abstract class deletedListClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            onclickManager((Integer)v.getTag(),v);
        }
        public abstract void onclickManager(int  position, View view);
    }



}
